package com.example.nextstream.api;


import com.alibaba.fastjson.JSONObject;
import com.example.nextstream.annotations.AuthRequired;
import com.example.nextstream.annotations.CurrentUser;
import com.example.nextstream.models.Properties;
import com.example.nextstream.models.User;
import com.example.nextstream.service.PropertiesService;
import com.example.nextstream.utils.RspBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/v1/properties")
public class PropertiesApi {

    @Autowired
    private PropertiesService propertiesService;

    //@RequestMapping("createProperties")
    //@AuthRequired
    //@CrossOrigin
    //public JSONObject createProperties(@RequestParam("Name") String name,@RequestParam("Value") String value){
    //    Properties properties = new Properties();
    //    properties.setName(name);
    //    properties.setValue(value);
    //    propertiesService.createProperties(properties);
    //    return RspBuilder.createSuccessRsp().build();
    //}

    @RequestMapping("updateRDPProperties")
    @AuthRequired
    @CrossOrigin
    public JSONObject updateRDPProperties(@RequestParam(value = "driveName", required = false, defaultValue = "Next-Stream") String driveName,
                                          @RequestParam(value = "drivePath", required = false, defaultValue = "./drive") String drivePath,
                                          @RequestParam(value = "enableDrive", required = false, defaultValue = "true") String enableDrive,
                                          @RequestParam(value = "enableTheming", required = false, defaultValue = "false") String enableTheming,
                                          @RequestParam(value = "enableWallpaper", required = false, defaultValue = "false") String enableWallpaper,
                                          @RequestParam(value = "enableFontSmoothing", required = false, defaultValue = "false") String enableFontSmoothing,
                                          @RequestParam(value = "enableMenuAnimations", required = false, defaultValue = "false") String enableMenuAnimations,
                                          @RequestParam(value = "enableDesktopComposition", required = false, defaultValue = "false") String enableDesktopComposition,
                                          @RequestParam(value = "enableFullWindowDrag", required = false, defaultValue = "false") String enableFullWindowDrag,
                                          @RequestParam(value = "disableGlyphCaching", required = false, defaultValue = "false") String disableGlyphCaching,
                                          @RequestParam(value = "disableOffscreenCaching", required = false, defaultValue = "false") String disableOffscreenCaching,
                                          @RequestParam(value = "disableBitmapCaching", required = false, defaultValue = "false") String disableBitmapCaching) {

        propertiesService.updateProperties(new Properties("drive-name", driveName));
        propertiesService.updateProperties(new Properties("drive-path", drivePath));
        propertiesService.updateProperties(new Properties("enable-drive", enableDrive));
        propertiesService.updateProperties(new Properties("enable-theming", enableTheming));
        propertiesService.updateProperties(new Properties("enable-wallpaper", enableWallpaper));
        propertiesService.updateProperties(new Properties("enable-font-smoothing", enableFontSmoothing));
        propertiesService.updateProperties(new Properties("enable-menu-animations", enableMenuAnimations));
        propertiesService.updateProperties(new Properties("enable-desktop-composition", enableDesktopComposition));
        propertiesService.updateProperties(new Properties("enable-full-window-drag", enableFullWindowDrag));
        propertiesService.updateProperties(new Properties("disable-glyph-caching", disableGlyphCaching));
        propertiesService.updateProperties(new Properties("disable-offscreen-caching", disableOffscreenCaching));
        propertiesService.updateProperties(new Properties("disable-bitmap-caching", disableBitmapCaching));

        return RspBuilder.createSuccessRsp().build();
    }

    @RequestMapping("updateSSHProperties")
    @AuthRequired
    @CrossOrigin
    public JSONObject updateSSHProperties(@RequestParam(value = "fontSize", required = false, defaultValue = "12") String fontSize,
                                          @RequestParam(value = "colorScheme", required = false, defaultValue = "gray-black") String colorScheme,
                                          @RequestParam(value = "fontName", required = false, defaultValue = "menlo") String fontName) {

        propertiesService.updateProperties(new Properties("font-size", fontSize));
        propertiesService.updateProperties(new Properties("color-scheme", colorScheme));
        propertiesService.updateProperties(new Properties("font-name", fontName));
        return RspBuilder.createSuccessRsp().build();
    }
    @RequestMapping("getRDPProperties")
    @AuthRequired
    @CrossOrigin
    public JSONObject getAllProperties(){
        // List<Properties> allProperties = propertiesService.getProperties();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("enableDrive", propertiesService.getPropertiesById("enable-drive"));
        jsonObject.put("driveName", propertiesService.getPropertiesById("drive-name"));
        jsonObject.put("disableBitmapCaching", propertiesService.getPropertiesById("disable-bitmap-caching"));
        jsonObject.put("disableGlyphCaching", propertiesService.getPropertiesById("disable-glyph-caching"));
        jsonObject.put("disableOffscreenCaching", propertiesService.getPropertiesById("disable-offscreen-caching"));
        jsonObject.put("drivePath", propertiesService.getPropertiesById("drive-path"));
        jsonObject.put("enableDesktopComposition", propertiesService.getPropertiesById("enable-desktop-composition"));
        jsonObject.put("enableFontSmoothing", propertiesService.getPropertiesById("enable-font-smoothing"));
        jsonObject.put("enableFullWindowDrag", propertiesService.getPropertiesById("enable-full-window-drag"));
        jsonObject.put("enableMenuAnimations", propertiesService.getPropertiesById("enable-menu-animations"));
        jsonObject.put("enableTheming", propertiesService.getPropertiesById("enable-theming"));
        jsonObject.put("enableWallpaper", propertiesService.getPropertiesById("enable-wallpaper"));
        return RspBuilder.createSuccessRsp().append("properties",jsonObject).build();
    }
    @RequestMapping("getSSHProperties")
    @AuthRequired
    @CrossOrigin
    public JSONObject getSSHProperties(){
        // List<Properties> allProperties = propertiesService.getProperties();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("fontSize",propertiesService.getPropertiesById("font-size"));
        jsonObject.put("fontName",propertiesService.getPropertiesById("font-name"));
        jsonObject.put("colorScheme",propertiesService.getPropertiesById("color-scheme"));
        return RspBuilder.createSuccessRsp().append("properties",jsonObject).build();
    }


}
